package com.hong.qrpc.strategy.client.config;

import com.hong.config.constant.ClientConfig;
import com.hong.qrpc.RpcClient;
import com.hong.qrpc.config.ConfigManager;
import com.hong.qrpc.constant.ProtoTypeConst;
import com.hong.qrpc.strategy.PropertiesStrategy;
import com.hong.qrpc.utils.StringUtils;

import java.util.Map;

/**
 * @Author HQT
 * @Date 2022/8/10 16:27
 */
public class ProtoTypeStrategy implements PropertiesStrategy {
    @Override
    public void disposeProperties(RpcClient rpcClient) {
        ConfigManager configManager = rpcClient.getConfigManager();
        Map<Object, Object> profile = configManager.getProfile();
        String protoType = (String) profile.get(ClientConfig.QRPC_PROTOCOL_TYPE);
        if (!StringUtils.isEmpty(protoType)) {
            if (ProtoTypeConst.isSupport(protoType)) {
                configManager.getRpcClientConfig().setProtocolType(ProtoTypeConst.getProtoTypeNum(protoType));
            } else {
                throw new IllegalArgumentException("unsupported protocol");
            }
        }
    }
}
